package cn.springcloud.book.security;

import cn.springcloud.book.service.SysUserService;
import cn.springcloud.book.vo.SysUserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author: yukong
 * @date: 2018/10/9 10:05
 * @description: security UserDetailsService实现类
 */
@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //模拟从数据库加载
        SysUserService sysUserService = new SysUserService();

        SysUserVo sysUserVo = sysUserService.loadUserByUsername(username);
        if (sysUserVo == null) {
            throw new UsernameNotFoundException("当前用户尚未注册");
        }

        return new UserDetailsImpl(sysUserVo);
    }
}
